<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Ext_detail_barang_masuk extends MY_Controller {
	
	function __construct() {
		parent::__construct(); 
		
		$this->load->model('ext_detail_barang_masuk_model', 'DBarang');
		$this->load->model('ext_combo_model', 'Combo');
		$this->secure_session();
	}

	function getdata($id_detail_barang_pk) {
		$this->secure_session();
		if ($id_detail_barang_pk != "") {
			echo $this->DBarang->GetJSON($id_detail_barang_pk);
		}
	}

	function ajax_post($id_barang_masuk_fk) {
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";



		$error = $this->validation();
		try {
			if (count( $error->Get() ) > 0) { 
				$is_error = 1; 
			} else {
				// Fill Post Data
				$data	= post_by_key(array('id_kondisi_fk','id_barang_fk' ,'harga','jumlah_barang','id_detail_barang_pk','total'));
				//$data['id_detail_barang_pk']	= @ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim($data['id_detail_barang_pk'])));
				// Set Date Format
				$data['id_barang_masuk_fk'] = $id_barang_masuk_fk;
				$data['total']	= (float)toNumber($data['total']);
				$data['harga']	= (float)toNumber($data['harga']);
				$data['jumlah_barang']	= (float)toNumber($data['jumlah_barang']);
				
				if (post("mode") == "create") {
					$data['tgl_rekam']	= NOW;
					$data['user_rekam']	= USERID;
					$data['id_detail_barang_pk'] = $this->DBarang->GenerateId();
					/*$data['harga']	= (float)toNumber($data['harga']);
					$data['jumlah_barang']	= (float)toNumber($data['jumlah_barang']);
					$data['total']	= (float)toNumber($data['total']);
*/
					$this->DBarang->Add($data);

					$ls_response = "Data Tersimpan";
					//App.audit_trail("CS001", "create/" + (data.app_id ?? ""));

				} else if (post("mode") == "edit") {
					$data['tgl_ubah']	= NOW;
					$data['user_ubah']	= USERID;
					/*$data['harga']	= (float)toNumber($data['harga']);
					$data['jumlah_barang']	= (float)toNumber($data['jumlah_barang']);
					$data['total']	= (float)toNumber($data['total']);
					*/
					$this->DBarang->Update($data, 
										array('id_detail_barang_pk'	=> $data['id_detail_barang_pk']));

					$ls_response = "Data Tersimpan";
					//App.audit_trail("CS002", "edit/" + (data.app_id ?? ""));

				}
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	private function validation() {
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");


		// Validation
		// if (post("sekolah") == '') {
		// 	$error->Add("dtl_pendidikan_sekolah", "Nama", $tpl_empty);
		// }

		return $error;
	}

	function ajax_delete() {
		$ls_result	= "";
		$id_detail_barang_pk	= get('id_detail_barang_pk');
		
		if ($id_detail_barang_pk != "") {
			// Delete Single
			$arr_rule	= array('id_detail_barang_pk' => $id_detail_barang_pk);
			$ls_result = $this->DBarang->DeleteSingle($arr_rule);

		} else {
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++) {
				$row	= $this->data['deleted'][$i];
				
				$arr_rule	= array('id_detail_barang_pk' => $row['id_detail_barang_pk']);
				$ls_result .= $this->DBarang->DeleteSingle($arr_rule);
			}
		}
		
		echo $ls_result;
	}

}
?>